package com.yangff.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangff.anno.LogExecuteTime;
import com.yangff.entity.product.entity.vo.ProductVo;
import com.yangff.entity.vo.ResultVo;

import com.yangff.entity.product.entity.Financialproduct;
import com.yangff.entity.product.entity.dto.AutoStockDTO;
import com.yangff.entity.product.entity.dto.FinancialproductDTO;
import com.yangff.service.IFinancialproductService;
import com.yangff.utils.MathUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 存储各种理财产品的详细信息 前端控制器
 * </p>
 *
 * @author yangff
 * @since 2025-01-15
 */
@RestController
@RequestMapping("/financialproduct")
@RequiredArgsConstructor
@Slf4j
@LogExecuteTime
public class FinancialproductController {

    private final IFinancialproductService financialproductService;

    //准确添加基金
    @CachePut(value = "product", key = "#financialproduct.code")
    @PostMapping
    public ProductVo saveProduct(@RequestBody Financialproduct financialproduct){
        financialproduct.setRiskLevel(MathUtil.getRiskLevel(financialproduct.getSigma(),0.01,0.3));
        financialproductService.save(financialproduct);

        return BeanUtil.copyProperties(financialproduct, ProductVo.class);
    }

    @GetMapping("/select-type")
    public Page<ProductVo> getProductList(@RequestParam Integer type, @PageableDefault Pageable pageable) {
        return financialproductService.getProductList(Page.of(pageable.getPageNumber() + 1, pageable.getPageSize()), String.valueOf(type));
    }

    @Transactional(rollbackFor = Exception.class)
    @CachePut(value = "product", key = "#stockDTO.code")
    @PostMapping("/type")
    public ProductVo saveTypeProduct(@RequestBody AutoStockDTO stockDTO){
        Financialproduct financialproduct = financialproductService.CreateRandomStock(stockDTO);
        financialproductService.save(financialproduct);
        return BeanUtil.copyProperties(financialproduct, ProductVo.class);
    }

    //按照产品代码查询
    @Cacheable(value = "product", key = "#code")
    @GetMapping("/{code}")
    public ProductVo getProductByCode(@PathVariable String code){
        Financialproduct financialproduct = financialproductService.getOne(new LambdaQueryWrapper<Financialproduct>().eq(Financialproduct::getCode,code));
        ProductVo productVo = BeanUtil.copyProperties(financialproduct, ProductVo.class);
        return productVo;
    }


    @GetMapping
    public Page<ProductVo> selectProduct(@PageableDefault Pageable pageable){
        // 创建 MyBatis-Plus 的 Page 对象
        Page<Financialproduct> page = new Page<>(pageable.getPageNumber() + 1, pageable.getPageSize());

        // 使用 Service 进行分页查询
        IPage<Financialproduct> financialproductPage = financialproductService.page(page);

        // 将 Financialproduct 转换为 ProductVo
        Page<ProductVo> productVoPage = new Page<>(financialproductPage.getCurrent(), financialproductPage.getSize(), financialproductPage.getTotal());
        productVoPage.setRecords(BeanUtil.copyToList(financialproductPage.getRecords(), ProductVo.class));

        return productVoPage;
    }

    //按照产品代码删除
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "product", key = "#code")
    @DeleteMapping("/{code}")
    public ResultVo deleteProductByCode(@PathVariable String code){
        financialproductService.remove(new LambdaQueryWrapper<Financialproduct>().eq(Financialproduct::getCode,code));
        return ResultVo.success();
    }

    //修改
    @Transactional(rollbackFor = Exception.class)
    @CachePut(value = "product", key = "#financialproductDTO.code")
    @PutMapping
    public ProductVo updateProduct(@RequestBody FinancialproductDTO financialproductDTO){
        Financialproduct financialproduct = financialproductService.updateFinancialproductByCode(financialproductDTO);
        financialproductService.update(financialproduct, new LambdaUpdateWrapper<Financialproduct>().eq(Financialproduct::getCode,financialproductDTO.getCode()));
        return BeanUtil.copyProperties(financialproduct, ProductVo.class);
    }



}
